<?php

/**
* AdminCodeListTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminCodeListTest extends WebTestCase
{
    private $url;
    private $testNAme = array();
    private $idArray = array();
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url =$global['service_url'];
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testDesc[] = md5(uniqid(rand(), true));
        $this->code = substr(md5(uniqid(rand(), true)),0,6);
        $locale =  &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->locale = $locale['default_locale']; 

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide ( code, owner, publish ) VALUES ('".substr(md5(uniqid(rand(), true)),0,6)."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName[0]."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        
        $this->idArray[$this->testName[0]] = $lastInsertId;

        $dbh->exec("INSERT INTO guide ( code, owner, publish ) VALUES ('".substr(md5(uniqid(rand(), true)),0,6)."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName[1]."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->idArray[$this->testName[1]] = $dbh->lastInsertId();

        $dbh->exec("INSERT INTO guide ( code, owner, publish ) VALUES ('".substr(md5(uniqid(rand(), true)),0,6)."','".$this->username."','1')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testName[2]."','".$this->locale."')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->idArray[$this->testName[2]] = $dbh->lastInsertId();


        $this->get($this->url.'?cmd=AdminCodeList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

    }

    public function testCodeList()
    {
        $this->assertTitle('Polowanie na kody Administracja');
        $this->assertLink('[Dodaj kod]',$this->url."index.php?cmd=AdminAddCode");
        $this->assertLink($this->testName[0],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[0]]);
        $this->assertLink($this->testName[1],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[1]]);
        $this->assertLink($this->testName[2],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[2]]);
        
    }

    public function testCodeListTranslation()
    {
        $this->get($this->url.'?cmd=AdminCodeList&locale=en_GB');
        $this->assertTitle('Code hunt Admin');
        $this->assertLink('[Add code]',$this->url."index.php?cmd=AdminAddCode");
        $this->assertLink('No translation for en_GB',$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[0]]);
        //Add en_GB translation
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$this->idArray[$this->testName[0]]."','".$this->testName[0]."','en_GB')");
        $dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$this->idArray[$this->testName[0]]."','".$this->testDesc."','en_GB')");

        $this->get($this->url.'?cmd=AdminCodeList&locale=en_GB');
        $this->assertTitle('Code hunt Admin');
        $this->assertLink('[Add code]',$this->url."index.php?cmd=AdminAddCode");
        $this->assertLink($this->testName[0],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[0]]);


    }    
    
    public function testRemoveCode()
    {

        $this->assertTitle('Polowanie na kody Administracja');
        
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[0]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[0]]);
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[1]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[1]]);
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[2]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[2]]);


        $this->assertLink('[Usuń]');
        $this->clickLinkById('AdminRemoveCode'.$this->idArray[$this->testName[0]]);
        $this->assertText('Usunąć kod "'.$this->testName[0].'" ?');
        $this->clickSubmit('Tak');

        $this->assertText('Kod usunięty.');
        

        $this->get($this->url.'?cmd=AdminCodeList');

        $this->assertLink($this->testName[1],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[1]]);
        $this->assertLink($this->testName[2],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[2]]);        
        $this->assertFalse($this->clickLink($this->testName[0],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[0]]));
        
        $this->assertFalse( $this->clickLinkById('AdminRemoveCode'.$this->idArray[$this->testName[0]]) );
    
        $this->assertTrue($this->clickLink('[Usuń]'));
    }    


    public function testDoNotRemoveCode()
    {
        $this->assertTitle('Polowanie na kody Administracja');
        
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[0]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[0]]);
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[1]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[1]]);
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[2]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[2]]);

        $this->clickLinkById('AdminRemoveCode'.$this->idArray[$this->testName[0]]);

        $this->clickSubmit('Nie');

        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[0]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[0]]);
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[1]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[1]]);
        $this->assertLinkById('AdminRemoveCode'.$this->idArray[$this->testName[2]],$this->url."index.php?cmd=AdminRemoveCode&id=".$this->idArray[$this->testName[2]]);

        $this->assertLink('[Dodaj kod]',$this->url."index.php?cmd=AdminAddCode");
        $this->assertLink($this->testName[0],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[0]]);
        $this->assertLink($this->testName[1],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[1]]);
        $this->assertLink($this->testName[2],$this->url."index.php?cmd=AdminAddCode&id=".$this->idArray[$this->testName[2]]);
        

    }  

    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("DELETE FROM guide WHERE rowid='".$this->idArray[$this->testName[0]]."'" );
        $dbh->exec("DELETE FROM guide WHERE rowid='".$this->idArray[$this->testName[1]]."'" );
        $dbh->exec("DELETE FROM guide WHERE rowid='".$this->idArray[$this->testName[2]]."'" );

        $dbh->exec("DELETE FROM guide_name WHERE guide_id='".$this->idArray[$this->testName[0]]."'" );
        $dbh->exec("DELETE FROM guide_name WHERE guide_id='".$this->idArray[$this->testName[1]]."'" );
        $dbh->exec("DELETE FROM guide_name WHERE guide_id='".$this->idArray[$this->testName[2]]."'" );


        $dbh->exec("DELETE FROM guide_desc WHERE guide_id='".$this->idArray[$this->testName[0]]."'" );
        $dbh->exec("DELETE FROM guide_desc WHERE guide_id='".$this->idArray[$this->testName[1]]."'" );
        $dbh->exec("DELETE FROM guide_desc WHERE guide_id='".$this->idArray[$this->testName[2]]."'" );

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminCodeList&logout=1');
        $dbh = null;
    }
    
}
?>
